import { observable, action } from 'mobx';

import { ListViewPage, SearchAdvanced } from 'weaver-mobile-page';
import { Bubble,Tools,Modal } from 'weaver-mobile'
import * as Api from '../../api/CenterApi';
import { PARAMS } from '../../util';

const getLabel = Tools.getLabel;
const alert = Modal.alert;


export default class MonitorStore {

  @observable themevisible = false;
  @observable themeLoading = true;
  @observable themeSearch = new SearchAdvanced.Store({ route: '/center/theme', id: 'themeSearch' }); //主题监控-高级搜索
  @observable themeList = new ListViewPage.Store({ route: '/center/theme', id: 'theme' });  // 主题监控列表
  @observable showThemeCheck = false; //主题监控-批量操作
  @observable themeAdValue = ''; //高级搜索值受控
  @observable optIndex= 1;
  @observable viewRight = 0;//页面权限
	@observable currentModel = {}; //当前对话框弹窗


  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  /*************************************************** 主题监控 ***************************************************/
  @action('主题监控请求')
  getCoworkList = (params) => {
    const themeSearchParams = this.themeSearch.form.formParams || {};
    params = {
      mainid: '',
      datetype: 'all',
        ...themeSearchParams,
        ...params,
    }
    Api.getCoworkItemMonitorList(params).then(res => {
      const dataKey = res.sessionkey;
      this.viewRight = res.viewRight
      if(this.viewRight === 0 ){
        this.themeLoading = false;
      }
      else if (dataKey) {
        this.dataKey = dataKey;
        this.themeList.getData({ dataKey }, ()=> this.themeLoading = false, true); // ture是否刷新用
      }
    })
  }

  
  @action("主题监控高级搜索")
  getCoworkItemMonitorShareCondition = () => {
  this.themeSearch.getData({
    url: '/api/cowork/itemMonitor/getCoworkItemMonitorShareCondition',
    method: 'GET',
    params: {...PARAMS}
  })
  }

  @action('主题监控置顶')
  batchCoworkItemMonitorTop = (params) => {
    Api.batchCoworkItemMonitorTop(params).then(res => {
      const status = res.status;
      if (status=== "1") {
        this.getCoworkList();
        this.doThemeCancel();
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
      }
    })
  }

  @action('主题监控取消置顶')
  batchCoworkItemMonitorCancelTop = (params) => {
    Api.batchCoworkItemMonitorCancelTop(params).then(res => {
      const status = res.status;
      if (status=== "1") {
        this.getCoworkList();
        this.doThemeCancel();
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
      }
    })
  }

  @action('主题监控结束')
  batchCoworkItemMonitorEnd = (params) => {
    Api.batchCoworkItemMonitorEnd(params).then(res => {
      const status = res.status;
      if (status=== "1") {
        this.getCoworkList();
        this.doThemeCancel();
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
      }
    })
  }
  
  
  @action('主题监控打开')
  coworkItemOperation = (params) => {
    Api.coworkItemOperation(params).then(res => {
      const status = res.status;
      if (status=== "1") {
        this.getCoworkList();
        this.doThemeCancel();
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
      }
    })
  }
  
  @action('主题监控删除')
  batchCoworkItemMonitorDel = (params) => {
    this.currentModel = alert(getLabel(388758, '确认要删除吗？'),'', [
      { text: getLabel(31129, '取消') },
      {
        text: getLabel(33703, '确认'),
        onPress: () => {
          Api.batchCoworkItemMonitorDel(params).then(res => {
            const status = res.status;
            if (status=== "1") {
              this.getCoworkList();
              this.doThemeCancel();
              Bubble.success(getLabel(30700, '操作成功')+'!', 1);
            }
          })
        }
      },
    ])
  }
  
  // 主题监控 -- 高级搜索内外name统一
  @action updateThemeAdvance = (value) => {
    this.themeAdValue = value;
    this.themeSearch.form.updateField('name', value);
  }

  // 主题监控 -- 取消
  @action doThemeCancel = () => {
    this.showThemeCheck = false;
    this.doThemeselectNone();
  }

  // 主题监控 -- 全选
  @action doThemeSelectAll = () => this.themeList.selectAll();

  // 主题监控 -- 取消全选
  @action doThemeselectNone = () => this.themeList.selectNone();

  // 主题监控 -- 高级搜素 -- 重置
  @action doThemeReset = () => {
    this.themeSearch.form.resetFields();
    this.themeAdValue = '';
    this.getCoworkList();
  }

  // 主题监控 -- 组件挂载时处理
  @action doThemeMount = () => {
    this.themeSearch.form.resetFields();
    this.themeAdValue = '';
	}
	
	//关闭所有弹窗
	@action closeAllModel = () => {
		this.currentModel && typeof this.currentModel.close === 'function' && this.currentModel.close();
	}
}